package com.jas.common;
//@author A0098803W
import java.io.IOException;
import java.util.Properties;
import java.util.TreeMap;

//@author A0098803W
/**
 * This is the class responsible for the program text display. It allows users
 * to switch to their local language.
 * 
 */
public final class Language {

	private static final String UNDEFINED_TEXT = "ERROR: Undefined text";
	private static Properties langConfigFile = new Properties();
	public static String currentLanguage = "eng";
	private static boolean setLanguage = false;
	private static TreeMap<String, Integer> tm = new TreeMap<String, Integer>();

	//@author A0098803W
	private static void setLanguageSupport() {
		tm.put("eng", 1);
		tm.put("english", 1);
		tm.put("chi", 2);
		tm.put("chinese", 2);
		tm.put("mandarin", 2);
		tm.put("may", 3);
		tm.put("malay", 3);
		tm.put("bahasa malaysia", 3);
		tm.put("bahasa indonesia", 3);
		tm.put("swe", 4);
		tm.put("swedish", 4);
		setLanguage = true;
	}

	//@author A0098803W
	/**
	 * This method loads the appropriate language translation based on user's
	 * input. Default language is English.
	 */
	public static void loadLanguageFile() {
		if (!setLanguage) {
			setLanguageSupport();
		}
		try {
			int switchLanguage = tm.get(currentLanguage);
			switch (switchLanguage) {
			case 1:

				langConfigFile
						.load(Language.class
								.getClassLoader()
								.getResourceAsStream(
										"com/jas/language/language.english.properties"));
				System.out.println(Language.getString("CURRENT_LANGUAGE"));
				break;
			case 2:
				langConfigFile
						.load(Language.class
								.getClassLoader()
								.getResourceAsStream(
										"com/jas/language/language.chinese.properties"));
				System.out.println(Language.getString("CURRENT_LANGUAGE"));
				break;
			case 3:
				langConfigFile.load(Language.class.getClassLoader()
						.getResourceAsStream(
								"com/jas/language/language.malay.properties"));
				System.out.println(Language.getString("CURRENT_LANGUAGE"));
				break;
			case 4:
				langConfigFile
						.load(Language.class
								.getClassLoader()
								.getResourceAsStream(
										"com/jas/language/language.swedish.properties"));
				System.out.println(Language.getString("CURRENT_LANGUAGE"));
				break;

			default:
				langConfigFile
						.load(Language.class
								.getClassLoader()
								.getResourceAsStream(
										"com/jas/language/language.english.properties"));
				System.out.println(Language.getString("CURRENT_LANGUAGE"));
				break;

			}

		} catch (Exception e) {
			try {
				langConfigFile
						.load(Language.class
								.getClassLoader()
								.getResourceAsStream(
										"com/jas/language/language.english.properties"));
				System.out.println(Language.getString("CURRENT_LANGUAGE"));
			} catch (IOException e1) {
				System.out.println(UNDEFINED_TEXT);
			}
		}

	}

	//@author A0098803W
	/**
	 * This method gets the text stored in the PROPERTIES file
	 * 
	 * @param text
	 * @return
	 */
	public static String getString(String text) {
		String translation = UNDEFINED_TEXT;
		try {
			translation = langConfigFile.getProperty(text);
		} catch (Exception e) {
			translation = "eng";

		}
		return translation;
	}

}
